using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VFX;

public class PlayerController : MonoBehaviour
{
    public PlayerControls playerControls;
    public Vector2 moveInput;
    public float moveSpeed = 5f;
    private Rigidbody2D rigidbody;
    private Animator playerAnimator;
    public VisualEffect visualEffect;
    private Material maskMaterial;
    [Header("shader可视半径")] public float visibleRadius;

    public Transform movePosition;

    public LayerMask wallMask;
    public LayerMask monsterMask;

    private void Awake()
    {
        playerControls = new PlayerControls();
        rigidbody = GetComponent<Rigidbody2D>();
        playerAnimator = GetComponent<Animator>();
        visualEffect = FindObjectOfType<VisualEffect>();
    }

    // Start is called before the first frame update
    void Start()
    {
        movePosition.parent = null;
        //movePosition.position += new Vector3(0, 1, 0);
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, movePosition.position, moveSpeed * Time.deltaTime);
        moveInput = playerControls.Gameplay.Movement.ReadValue<Vector2>().normalized;
        //设置动画
        if (moveInput == Vector2.zero)
        {
            playerAnimator.SetBool(Settings.isMoving, false);
            return;
        }

        if (moveInput.x > 0 && moveInput.y > 0 || moveInput.x > 0 && moveInput.y < 0 ||
            moveInput.x < 0 && moveInput.y > 0 || moveInput.x < 0 && moveInput.y < 0)
        {
            playerAnimator.SetBool(Settings.isMoving, false);
            return;
        }

        MoveCharacter(moveInput);
    }


    private void MoveCharacter(Vector2 direction)
    {
        playerAnimator.SetBool(Settings.isMoving, true);
        // rigidbody.MovePosition((Vector2)transform.position + direction * moveSpeed * Time.deltaTime);
        playerAnimator.SetFloat(Settings.moveX, direction.x);
        playerAnimator.SetFloat(Settings.moveY, direction.y);
        //visualEffect.SetVector3("PlayerPos",transform.position);
        if (Vector3.Distance(transform.position, movePosition.position) <= 0.05f)
        {
            if (!Physics2D.OverlapCircle((Vector2)movePosition.position + direction, 0.2f, monsterMask))
            {
                if (direction.x > 0 && !Physics2D.OverlapCircle((Vector2)movePosition.position+new Vector2(0.5f,0),0.2f,wallMask)
                    ||direction.x < 0 && !Physics2D.OverlapCircle((Vector2)movePosition.position+new Vector2(-0.5f,0),0.2f,wallMask)
                    ||direction.y > 0 && !Physics2D.OverlapCircle((Vector2)movePosition.position+new Vector2(0,0.5f),0.2f,wallMask)
                    ||direction.y < 0 && !Physics2D.OverlapCircle((Vector2)movePosition.position+new Vector2(0,-0.5f),0.2f,wallMask))
                {
                    movePosition.position += new Vector3(direction.x, direction.y, 0);
                }
            }
            else
            {
                Direction dir = Direction.Right;
                if (direction.x != 0)
                {
                    dir = direction.x > 0 ? Direction.Right : Direction.Left;
                }
                else if (direction.y != 0)
                {
                    dir = direction.y > 0 ? Direction.Up : Direction.Down;
                }

                Collider2D overlapCircle =
                    Physics2D.OverlapCircle((Vector2)movePosition.position + direction, 0.2f, monsterMask);
                Monster monster = overlapCircle.gameObject.GetComponent<Monster>();
                BattleSystem.Instance.SetMonster(monster, dir);
                BattleSystem.Instance.StartBattle();
            }
        }
    }

    void OnDrawGizmos() {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, 0.5f);
    }

    private void OnEnable()
    {
        playerControls.Gameplay.Enable(); // 启用Player Action Map
    }

    private void OnDisable()
    {
        playerControls.Gameplay.Disable();
    }
}